﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using GKFX.Common.Business.BOL.Common;
using Telerik.Web.Mvc;
using Century.Web.Security.Attributes
using GKFX.Common.UI.MVC;

namespace Century.Web.MVC.AdminArea
{
    //[Kooboo.CMS.Web.Authorizations.Authorization(AreaName = "AdminTools", Group = "", Name = "UserRoleManagement")]
    [Authorization(Group = "", Name = "Administration")]
    public class UserRoleManagementController : CenturyAdminBaseController
    {
        //
        // GET: /AdminTools/UserRoleManagement/
        UserBSO userBSO = new UserBSO();
        RoleBSO roleBSO = new RoleBSO();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetUserList()
        {
            var model = new GridModel
            {
                Data = userBSO.GetAll().Select(e => new { e.Id, e.UserName, e.FirstName, e.LastName, e.IsAdministrator })
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult EditUser(int id)
        {
            GKFX.Models.Common.User user = userBSO.Find(c => c.Id == id);
            TryUpdateModel(user);
            userBSO.Update(user);
            var model = new GridModel
            {
                Data = userBSO.GetAll().Select(e => new { e.Id, e.UserName, e.FirstName, e.LastName, e.IsAdministrator })
            };
            return View(model);
        }

        [GridAction]
        public ActionResult GetRolesForUser(int userId)
        {
            IEnumerable<GKFX.Models.Common.Role> roles = roleBSO.GetAll();
            IEnumerable<GKFX.Models.Common.Role> userRoles = userBSO.GetRolesForUser(userId);
            var confDetails = roles.Select(c =>
                new
                {
                    c.Id,
                    c.RoleName,
                    c.DisplayName,
                    UserHasThisRole = userRoles.Any(a => a.RoleName == c.RoleName)
                });
            return View(new GridModel(confDetails));
        }

        public string ChangeUserRole(int userId, int roleId, bool userHasThisRole)
        {
            GKFX.Models.Common.User user = userBSO.Find(c => c.Id == userId, "Roles");
            GKFX.Models.Common.Role role = userBSO.FindRole(roleId);
            if (userHasThisRole)
            {
                user.Roles.Add(role);
            }
            else
            {
                user.Roles.Remove(role);
            }
            userBSO.Update(user);
            return string.Empty;
        }

        [Authorization(Group = "", Name = "Administration")]
        public ActionResult ChangePasswordByAdmin(string userName)
        {

            return View(new KeyValuePair<string, string>(userName, string.Empty));
        }

        [Authorization(Group = "", Name = "Administration")]
        [HttpPost]
        public ActionResult ChangePasswordByAdmin(string UserName, string Value)
        {
            if (ModelState.IsValid)
            {

                // ChangePassword will throw an exception rather
                // than return false in certain failure scenarios.

                new GKFX.Web.Security.EFMembershipProvider().ChangePasswordByAdmin(UserName, Value);



                return RedirectToAction("Index");


            }

            // If we got this far, something failed, redisplay form
            return View();
        }

        //[HttpPost]
        //public ActionResult AddUser(RegisterModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        // Attempt to register the user
        //        MembershipCreateStatus createStatus;
        //        Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

        //        if (createStatus == MembershipCreateStatus.Success)
        //        {
        //            FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
        //            return RedirectToAction("Index", "Home");
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("", UIHelpers.ErrorCodeToString(createStatus));
        //        }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    return View(model);
        //}

        
    }
}
